var postsData = require('../../../data/post-data.js')
let app = getApp();
Page({
  data:({
    isplayngMusic: false
  }),
  onLoad:function(option){
    //获取到app.js的全局数据
    var globalData = app.globalData
    //接受post页面的传来的参数
    var postId = option.id;
    this.data.currentPostId = postId;
    this.setData({
      currentPostId: postId
    })
    var postData = postsData.postList[postId];
    //同步
    //this.data.postData = postData;
    //异步
    this.setData({
      postData:postData
    })
    
    var postsCollected = wx.getStorageSync('posts_collected')
    if (postsCollected) {
     
      var postsCollected = postsCollected[postId]
      this.setData({
        collected: postsCollected
      })
    }
    else {
      postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected);
    }
    
    //判断现在是不是在进行播放音乐和播放的是哪个页面的音乐
    if (app.globalData.g_isPlayMusic && app.globalData.g_currentMusicPostId === postId){
      this.setData({
        isplayngMusic: true
      })
    }
    //二度调用
    this.setMusicMonitor();
  },
  
  //音乐监听事件封装
  setMusicMonitor:function(){
    var that = this;
    //同步播放音乐事件
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isplayngMusic: true
      })
      app.globalData.g_isPlayMusic = true;
      //让全局POSTID为data下的当前id进行赋值
      app.g_currentMusicPostId = that.data.currentPostId;
    })
    //同步暂停音乐事件
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isplayngMusic: false
      })
      app.globalData.g_isPlayMusic = false;
      app.g_currentMusicPostId = null;
    })
    //音乐停止事件
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isplayngMusic: false
      })
      app.globalData.g_isPlayMusic = false;
      app.g_currentMusicPostId = null;
    })
  },
  
  oncollectionTap:function (event) {
    var postsCollected = wx.getStorageSync('posts_collected');
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    //更新文章是否的缓存值
    wx.setStorageSync('posts_collected', postsCollected);
    //更新数据绑定的变量, 从而实现切换图片
    this.setData({
      collected: postCollected
    })
    //用收藏的状态来决定
     wx.showToast({
       title: postCollected?"收藏成功":"收藏失败",
       duration: 1000,
     })
    
     //模态框
     /*wx.showModal({
       title: '收藏',
       content: '是否收藏该文章?',
       showCancel: true,
     })*/
  },

  //分享模块
  onShareTap:function(event){
    var itemList = [
      "分享到微博",
      "分享到朋友圈",
      "分享给QQ好友",
      "分享给微信好友"
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success:function(res){
        //res.cancel   用户点击了取消
        //res.tapIndex  数组元素的序号,从0开始
        wx.showModal({
          title: "用户" + itemList[res.tapIndex],
          content: "用户是否取消?"+res.cancel+'现在微信暂时无法分享',
        })
      }
    })
  },
  MusiconTap:function(event){
    var currentPostId = this.data.currentPostId;
    var isplayngMusic = this.data.isplayngMusic;
    if (isplayngMusic){
       wx.pauseBackgroundAudio();
      this.setData({
        isplayngMusic: false
      })
    }
    else{
      wx.playBackgroundAudio({
        dataUrl: postsData.postList[currentPostId].music.url,
        title: postsData.postList[currentPostId].music.title,
        coverImgUrl: postsData.postList[currentPostId].music.coverImg
      })
      this.setData({
        isplayngMusic:true
      })
    }
  }
})